package wiki.hitime.stl.daq.service.factory.impl.check;

import wiki.hitime.stl.daq.bean.bo.DataAcquisitionValueBO;
import wiki.hitime.stl.daq.bean.bo.DataCheckConfigBO;
import wiki.hitime.stl.daq.bean.bo.DataCheckResultBO;
import wiki.hitime.stl.daq.bean.bo.DataCheckValueBO;
import wiki.hitime.stl.daq.enums.DataAcquisitionTypeEnum;
import wiki.hitime.stl.daq.enums.DataValueTypeEnum;
import wiki.hitime.stl.daq.util.StrUtils;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.Arrays;

/**
 * @author zhangdaoping
 * @create 2019-07-09 18:49
 */
@Service
@Slf4j
public class GreaterThanDataCheckImpl extends AbstractDataCheckService {

    /**
     * 获取实现类暴露的配置信息
     *
     * @return 数据采集类型
     */
    @Override
    public DataCheckConfigBO getCheckConfig() {
        return DataCheckConfigBO.builder()
                .supportAcquisitionType(DataAcquisitionTypeEnum.SINGLE_VALUE_DATA.getStatus())
                .supportDataValueTypeList(Arrays.asList(
                        DataValueTypeEnum.NUMBER.getStatus(),
                        DataValueTypeEnum.DATETIME.getStatus()))
                .checkName("大于")
                .implServiceName(StrUtils.toLowerCase(this.getClass().getSimpleName(), 0))
                .build();
    }

    /**
     * 加载检查结果
     *
     * @param acquisitionValueBO 采集数据对象
     * @param dataCheckValueBO   检查数据对象
     * @return 检查结果对象
     */
    @Override
    public DataCheckResultBO loadCheckResult(DataAcquisitionValueBO acquisitionValueBO, DataCheckValueBO dataCheckValueBO) {
        validateArgs(acquisitionValueBO, dataCheckValueBO);
        String acquisitionValueStr = acquisitionValueBO.getDataList().get(0)[0];
        String checkValueStr = dataCheckValueBO.getDataList().get(0);
        Boolean checkResult = Boolean.FALSE;
        String checkSummary;
        DataValueTypeEnum valueTypeEnum = loadValueTypeEnum(acquisitionValueBO.getValueType());
        switch (valueTypeEnum) {
            case NUMBER: {
                BigDecimal acquisitionValue = valueFormatBigDecimal(acquisitionValueStr);
                BigDecimal checkValue = valueFormatBigDecimal(checkValueStr);
                checkResult = acquisitionValue.compareTo(checkValue) > 0;
                checkSummary = loadSummary(acquisitionValue, checkValue, checkResult);
            }
            break;
            case DATETIME: {
                String valueFormat = acquisitionValueBO.getValueFormat();
                Assert.hasText(valueFormat, "值转换表达式为空");
                DateTime acquisitionValue = valueFormatDateTime(acquisitionValueStr, valueFormat);
                DateTime checkValue = valueFormatDateTime(checkValueStr, valueFormat);
                checkResult = acquisitionValue.compareTo(checkValue) > 0;
                checkSummary = loadSummary(acquisitionValue, checkValue, checkResult);
            }
            break;
            case BOOLEAN:
            case STRING:
            default: {
                checkSummary = loadNotSupportSummary(valueTypeEnum);
            }
            break;
        }

        return DataCheckResultBO.builder()
                .checkResult(checkResult)
                .checkSummary(checkSummary)
                .build();
    }
}
